{% extends 'admin/base.html'%}

{% block body%}
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-toolbar clearfix">
                    <form class="pull-right search-bar" method="get" action="{{url_for('admin.index')}}" role="form">
                        <div class="input-group">
                            <input autocomplete="off" type="text" name="keywords" class="form-control" placeholder="请输入关键词...">
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-default" type="button">搜索</button>
                            </span>
                        </div>
                    </form>
                    <div class="toolbar-btn-action">
                        {%if current_user.have_permission('admin.add')%}<a class="btn btn-primary m-r-5" href="{{url_for('admin.add')}}"><i class="mdi mdi-plus"></i>
                            新增</a>{% endif%}
                        {%if current_user.have_permission('admin.status')%}<a class="btn btn-success m-r-5" href="javascript:void(0)" onclick="setOnOff(1)"><i
                                class="mdi mdi-check"></i> 启用</a>{% endif%}
                        {%if current_user.have_permission('admin.status')%}<a class="btn btn-warning m-r-5" href="javascript:void(0)" onclick="setOnOff(0)"><i
                                class="mdi mdi-block-helper"></i> 禁用</a>{% endif%}
                        {%if current_user.have_permission('admin.delete_op')%}<a class="btn btn-danger" href="javascript:void(0)" onclick="doDeleteAll()"><i
                                class="mdi mdi-window-close"></i> 删除</a>{% endif%}
                    </div>
                </div>
                <div class="card-body">

                    {% for message in get_flashed_messages() %}
                    <div class="alert alert-danger alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                                aria-hidden="true">×</span></button>
                        {{ message }}
                    </div>
                    {% endfor %}

                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        <label class="lyear-checkbox checkbox-primary">
                                            <input type="checkbox" id="check-all"><span></span>
                                        </label>
                                    </th>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>角色</th>
                                    <th>登录次数</th>
                                    <th>最后登录时间</th>
                                    <th>登录时间</th>
                                    <th>状态</th>
                                    <th width="130">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for admin in pagination.items %}
                                <tr>
                                    <td>
                                        <label class="lyear-checkbox checkbox-primary">
                                            <input type="checkbox" name="ids[]" value="{{admin.id}}"><span></span>
                                        </label>
                                    </td>
                                    <td>{{admin.id}}</td>
                                    <td>{{admin.username}}</td>
                                    <td>{{admin.role.name}}</td>
                                    <td>{{admin.times}}</td>
                                    <td>{{admin.last_login_entered}}</td>
                                    <td>{{admin.update_time}}</td>
                                    <td>
                                        {% if admin.status==1%}
                                        <font class="text-success">正常</font>
                                        {%else%}
                                        <font class="text-danger">禁止</font>
                                        {% endif%}
                                    </td>
                                    <td>
                                        <div class="btn-group">
                                            {%if current_user.have_permission('admin.edit')%}<a
                                                class="btn btn-primary btn-xs"
                                                href="{{url_for('admin.edit', id=admin.id)}}"><i
                                                    class="mdi mdi-pencil"></i>
                                                编辑</a>
                                            {% endif%}
                                            {%if current_user.have_permission('admin.delete_op')%}
                                            <a class="btn btn-xs btn-danger"
                                                href="javascriptvoid(0)" onclick="delete_op('admin.id')"><i
                                                    class="mdi mdi-window-close"></i>删除</a>
                                            {% endif%}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {{page_func.render_pagination(pagination, 'admin.index')}}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock%}

{%block foot%}
<script type="text/javascript">
    function getSelected() {
            var checkbox = $("tbody .lyear-checkbox > input")
            var ids = []
            checkbox.each(function (k, v) {
                if ($(this).is(":checked")) {
                    ids.push($(this).val())
                }
            })
            return ids.join(",")
        }

        function doDeleteAll() {
            var ids = getSelected()
            delete_op(ids)
        }

        function delete_op(ids) {
            $.confirm({
                title: '警告',
                content: '您确定要删除吗？',
                type: 'orange',
                typeAnimated: true,
                buttons: {
                    tryAgain: {
                        text: '确定',
                        btnClass: 'btn-red',
                        action: function () {
                            window.location.href = "{{url_for('admin.delete_op')}}?id=" + ids
                        }
                    },
                    close: {
                        text: '关闭'
                    }
                }
            });
        }
</script>
{% endblock%}